<?php
/**
 * 榜单数据修改控制器
 */
namespace Beerus\Modules\DesignerRanking\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\service\Member;
use Encore\Admin\Facades\Admin;
use Hhz\AdministrativeRegion\Region;
use Illuminate\Http\Request;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Widgets\Tab;
use Doraemon\model\DesignerRanking\DesignerRankingChangeLog;
use function Clue\StreamFilter\fun;

class RankingChangeLogController extends Controller
{

    const PAGE_SIZE = 10;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    // 列表
    public function index(Request $request, Content $content)
    {
        $page_size = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);

        $condition = [];
        if (!empty($request['ranking_month'])) {
            $condition['ranking_month'] = $request['ranking_month'];
        }
        if (!empty($request['assoc_type'])) {
            $condition['assoc_type'] = $request['assoc_type'];
        }
        if (!empty($request['uid'])) {
            $condition['uid'] = $request['uid'];
        }

        $condition['data_status'] = DesignerRankingChangeLog::STATUS_NORMAL;
        $total = DesignerRankingChangeLog::count($condition);
        $condition['ORDER'] = ['id' => 'DESC'];
        $condition['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size
        ];
        $lists = DesignerRankingChangeLog::dataSelect($condition);
        $uids = array_column($lists, 'uid');
        $memberList = Member::getUserInfoByUserIds($uids);
        $uidUserInfoMap = array_column($memberList, null, 'uid');

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);

        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel, $uidUserInfoMap) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('ranking_month', '榜单期号')->placeholder('请输入榜单期号');
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('assoc_type', '关联业务')->select([1=>'调整分数']);
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('uid', '用户ID')->placeholder('请输入用户ID');
                    });

                    $filter->expand();
                });

                $grid->column('id', 'ID');
                $grid->column('ranking_month', '榜单期号');
                $grid->column('user_info', '用户信息')->display(function () use ($uidUserInfoMap) {
                    $userInfo[] = '用户UID：'.$this->uid;
                    $userInfo[] = '用户昵称：'.$uidUserInfoMap[$this->uid]['nick'];
                    $userInfo[] = '当前用户身份：'.\Doraemon\model\Member::$typeDesc[$uidUserInfoMap[$this->uid]['type']];
                    $userInfo[] = '调整时用户身份：'.DesignerRankingChangeLog::USER_TYPE_DESC[$this->user_type];
                    return implode('<br/>', $userInfo);
                });
                $grid->column('ranking_info', '榜单信息')->display(function () {
                    $rankingInfo[] = '榜单城市：' . Region::getNameByCode($this->area_code);
                    $rankingInfo[] = '主榜单名称：' . $this->main_ranking_name;
                    $rankingInfo[] = '次榜单名称：' . $this->sub_ranking_name;
                    return implode('<br/>', $rankingInfo);
                });
                $grid->column('assoc_type', '业务类型')->display(function () {
                    return '调整分数';
                });
                $grid->column('old_data', '历史内容')->display(function () {
                    $data = json_decode($this->old_data, true);
                    $oldData[] = '调整前分数：'.$data['ranking_score'];
                    $oldData[] = '调整前排名：'.$data['ranking_no'];
                    return implode('<br/>', $oldData);
                });
                $grid->column('new_data', '更改后内容')->display(function () {
                    $data = json_decode($this->new_data, true);
                    $newData[] = '调整后分数：'.$data['ranking_score'];
                    $newData[] = '调整后排名：'.$data['ranking_no'];
                    return implode('<br/>', $newData);
                });
                $grid->column('admin_name', '操作员')->display(function () {
                    return Admin::user()->find($this->admin_id)->name;
                });
                $grid->column('operate_date', '操作时间')->display(function () {
                    return date('Y-m-d H:i:s', $this->add_time);
                });
                $grid->disableActions();
            }
        );

        $tab = new Tab();
        $tab->add('月榜分数调整记录', $gridList->render());
        $content->row($tab);
        return $content
            ->header('月榜分数调整记录')
            ->description(' ')
            ->breadcrumb(['text' => '月榜分数调整记录']);
    }
}
